﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Windows
{
	public abstract class StateProxy : IStateProxy
	{

		public TypeConverter GetConverter() { return TypeDescriptor.GetConverter(this, true); }
		public EventDescriptorCollection GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); }
		public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); }
		public string GetComponentName() { return TypeDescriptor.GetComponentName(this, true); }
		public object GetPropertyOwner(PropertyDescriptor pd) { return this; }
		public AttributeCollection GetAttributes() { return TypeDescriptor.GetAttributes(this, true); }
		public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return GetProperties(); }
		public string GetClassName() { return TypeDescriptor.GetClassName(this, true); }
		public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); }
		public PropertyDescriptor GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); }
		public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); }

		private bool m_ShowOnlyIfPresent = false;
		private bool m_ShowReadOnlyProperties = true;
		//private bool m_AllReadOnly = false;
		//[Browsable(false)]
		//public bool AllReadOnly
		//{
		//   get { return m_AllReadOnly; }
		//   set
		//   {
		//      if (m_AllReadOnly != value)
		//         m_Properties = null;
		//      m_AllReadOnly = value;
		//   }
		//}
		[Browsable(false)]
		public bool ShowOnlyIfPresent { get { return m_ShowOnlyIfPresent; }
			set
			{
				if (m_ShowOnlyIfPresent != value)
					m_Properties = null;
				m_ShowOnlyIfPresent = value;
			}
		}
		[Browsable(false)]
		public bool ShowReadOnlyProperties { get { return m_ShowReadOnlyProperties; }
			set
			{
				if (m_ShowReadOnlyProperties != value)
					m_Properties = null;
				m_ShowReadOnlyProperties = value;
			}
		}

		private PropertyDescriptorCollection m_Properties;
		public PropertyDescriptorCollection GetProperties()
		{
			if (m_Properties == null)
			{
				m_Properties = new PropertyDescriptorCollection(null);
				PropertyDescriptorCollection defaultProp = TypeDescriptor.GetProperties(this, true);
				foreach (PropertyDescriptor pd in defaultProp)
				{
					if (pd.IsReadOnly)
					{
						if (m_ShowReadOnlyProperties == true)
							m_Properties.Add(pd);
					}
					else if (m_ShowOnlyIfPresent)
					{
						if (pd.PropertyType == typeof(bool))
						{
							if ((bool)pd.GetValue(this) == true)
								m_Properties.Add(pd);
						}
						else if (pd.PropertyType == typeof(int))
						{
							if ((int)pd.GetValue(this) > 0)
								m_Properties.Add(pd);
						}
						else
							m_Properties.Add(pd);
					}
					else
					{
						m_Properties.Add(pd);
					}
				}
			}
			return m_Properties;
		}




		public abstract IState State { get; }
		public abstract TimeSpan Time { get; set; }
		public abstract int Food { get; }
		public abstract int Supply { get; }
		public abstract int Minerals { get; set; }
		public abstract int Gas { get; set; }
		public abstract int MineralsIncome { get; }
		public abstract int GasIncome { get; }
		public abstract int WorkersOnMinerals { get; }
		public abstract int WorkersOnGas { get; }
	}
}
